package com.gla.controller;

import com.gla.commom.Result;
import com.gla.entity.Admin;
import com.gla.service.LoginService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
@RequestMapping("/login")
public class LoginController {

    @Resource
    private CheckController checkCode;
    @Resource
    LoginService loginService;

    @GetMapping("/admin")
    public Result login(Admin admin){  //RequestBody将前端传过来的值映射成Java对象
        String username = admin.getUsername();
        String password = admin.getPassword();
        if(username == "" || password == "" || admin.getVerificationCode() == ""){
            return Result.fail("有未输入的");
        }else{
            String kaptchaCode = checkCode.getCheckCode();
            String rightCode = admin.getVerificationCode();
            if (kaptchaCode.equals(rightCode)){
                boolean isOK = loginService.isAdmin(username,password);
                if(isOK == true){
                    return Result.succeed(Result.succeed());
                }else{
                    return Result.succeed(Result.fail("账号或密码有误"));
                }

            }else {
                return Result.succeed(Result.verifica());
            }
        }
    }

}
